﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain
{
    /// <summary>
    /// Class used to Animate values staticly
    /// </summary>
    public static class AnimationUtil
    {
        /// <summary>
        /// Animates from one point to another.
        /// </summary>
        /// <param name="ActualPosition">The Actual value</param>
        /// <param name="Destiny">The destiny value</param>
        /// <param name="Division">The Division. The more, the slower.</param>
        /// <returns></returns>
        public static Rectangle SmoothAnimate(Rectangle ActualPosition, Rectangle Destiny, float Division)
        {
            ActualPosition.X = SmoothAnimate((int)ActualPosition.X, Destiny.X, Division);
            ActualPosition.Y = SmoothAnimate((int)ActualPosition.Y, Destiny.Y, Division);
            ActualPosition.Width = SmoothAnimate((int)ActualPosition.Width, Destiny.Width, Division);
            ActualPosition.Height = SmoothAnimate((int)ActualPosition.Height, Destiny.Height, Division);

            return ActualPosition;
        }


        public static Vector3 SmoothAnimate(Vector3 ActualPosition, Vector3 Destiny, float Division)
        {
            ActualPosition.X = SmoothAnimate((float)ActualPosition.X, Destiny.X, Division);
            ActualPosition.Y = SmoothAnimate((float)ActualPosition.Y, Destiny.Y, Division);
            ActualPosition.Z = SmoothAnimate((float)ActualPosition.Z, Destiny.Z, Division);

            return ActualPosition;
        }

        /// <summary>
        /// Animates from one point to another.
        /// </summary>
        /// <param name="ActualPosition">The Actual value</param>
        /// <param name="Destiny">The destiny value</param>
        /// <param name="Division">The Division. The more, the slower.</param>
        /// <returns></returns>
        public static Vector4 SmoothAnimate(Vector4 ActualPosition, Vector4 Destiny, float Division)
        {
            ActualPosition.X = SmoothAnimate((float)ActualPosition.X, Destiny.X, Division);
            ActualPosition.Y = SmoothAnimate((float)ActualPosition.Y, Destiny.Y, Division);
            ActualPosition.Z = SmoothAnimate((float)ActualPosition.Z, Destiny.Z, Division);
            ActualPosition.W = SmoothAnimate((float)ActualPosition.W, Destiny.W, Division);
            return ActualPosition;
        }

        /// <summary>
        /// Animates from one point to another.
        /// </summary>
        /// <param name="ActualPosition">The Actual value</param>
        /// <param name="Destiny">The destiny value</param>
        /// <param name="Division">The Division. The more, the slower.</param>
        /// <returns></returns>
        public static Color SmoothAnimate(Color ActualColor, Color Destiny, float Division)
        {
            ActualColor.R = (byte)SmoothAnimate((float)ActualColor.R, Destiny.R, Division);
            ActualColor.G = (byte)SmoothAnimate((float)ActualColor.G, Destiny.G, Division);
            ActualColor.B = (byte)SmoothAnimate((float)ActualColor.B, Destiny.B, Division);
            ActualColor.A = (byte)SmoothAnimate((float)ActualColor.A, Destiny.A, Division);
            return ActualColor;
        }

        /// <summary>
        /// Animates from one point to another.
        /// </summary>
        /// <param name="ActualPosition">The Actual value</param>
        /// <param name="Destiny">The destiny value</param>
        /// <param name="Division">The Division. The more, the slower.</param>
        /// <returns></returns>
        public static Vector2 SmoothAnimate(Vector2 ActualPosition, Vector2 Destiny, float Division)
        {
            ActualPosition.X = SmoothAnimate((float)ActualPosition.X, Destiny.X, Division);
            ActualPosition.Y = SmoothAnimate((float)ActualPosition.Y, Destiny.Y, Division);
            return ActualPosition;
        }

        /// <summary>
        /// Animates from one point to another.
        /// </summary>
        /// <param name="ActualPosition">The Actual value</param>
        /// <param name="Destiny">The destiny value</param>
        /// <param name="Division">The Division. The more, the slower.</param>
        /// <returns></returns>
        public static float SmoothAnimate(float ActualPosition, float Destiny, float Division)
        {
            float X = 0;
            if (Destiny > ActualPosition)
                X = (Destiny - ActualPosition) / Division;
            else
                X = (ActualPosition - Destiny) / Division;

            if (ActualPosition > Destiny)
                ActualPosition -= X;
            else if (ActualPosition < Destiny)
                ActualPosition += X;

            return ActualPosition;
        }

        /// <summary>
        /// Animates from one point to another. Be aware that int division might result in 0 values,
        /// resulting in no change to the Actual Position...
        /// </summary>
        /// <param name="ActualPosition">The Actual value</param>
        /// <param name="Destiny">The destiny value</param>
        /// <param name="Division">The Division. The more, the slower.</param>
        /// <returns></returns>
        public static int SmoothAnimate(int ActualPosition, int Destiny, float Division)
        {
            float final = ActualPosition;

            float X = 0;
            if (Destiny > final)
                X = (Destiny - final) / Division;
            else
                X = (final - Destiny) / Division;

            if (final > Destiny)
                final -= X;
            else if (final < Destiny)
                final += X;

            return (int)final;
        }
    }
}
